//
//  ReportBlock.m
//  Aschat
//
//  Created by luwei on 2023/4/14.
//  Copyright © 2023 luwei. All rights reserved.
//

#import "ReportBlock.h"

@implementation ReportBlock

- (instancetype)initWithIsBlock:(BOOL)isBlock isFriend:(BOOL)isFriend userId:(NSInteger)userId{
    if(self == [super init]){
        self.isBlock = 0;
        self.isFriend = 0;
        self.isBlock = isBlock;
        self.isFriend = isFriend;
        self.userId = userId;
    }
    return self;
}


- (void)show{
    //解决视频页，对方退出 本地未退出BUG
    NSMutableString *blockTitle = [[NSMutableString alloc]initWithFormat:@"Block"];
    NSMutableString *followTitle = [[NSMutableString alloc]initWithFormat:@"Follow"];
//    if(self.isBlock){
//        [blockTitle setString:@"unBlock"];
//    }
    if(self.isFriend){
        [followTitle setString:@"UnFollow"];
    }
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *report = [UIAlertAction actionWithTitle:@"Report" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self reportSure];
        NSMutableArray *dataList = [NSMutableArray array];
        NSDictionary *dict = @{
            @"page":@"report",
            @"duration":@(0),
            @"event":@"pv",
            @"tm":@([[BJNetManager shareNetManager] gs_getCurrentTimeToMilliSecond]),
        };
        [dataList addObject:dict];
        [[BJNetManager shareNetManager]postLogWithParam:dataList];
    }];
    UIAlertAction *block = [UIAlertAction actionWithTitle:blockTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        if([blockTitle isEqualToString:@"Block"]){
            NSMutableDictionary *param = [NSMutableDictionary dictionary];
            [param setObject:@(self.userId) forKey:@"broadcasterId"];
            [param setObject:@"Block" forKey:@"complainCategory"];
            [[BJNetManager shareNetManager]postBlockReportWithParam:param completionHandler:^(id  _Nonnull responOj, NSError * _Nonnull error) {
                NSDictionary *dict = responOj;
                NSInteger code = [dict[@"code"]integerValue];
                if(!error && code == 0){
                    [self SuccessFullyWithTitle:blockTitle];
                    if(self.blockClickBlock)self.blockClickBlock(YES);
                    NSMutableArray *dataList = [NSMutableArray array];
                    NSDictionary *dict = @{
                        @"page":@"block",
                        @"duration":@(0),
                        @"event":@"pv",
                        @"tm":@([[BJNetManager shareNetManager] gs_getCurrentTimeToMilliSecond]),
                    };
                    [dataList addObject:dict];
                    [[BJNetManager shareNetManager]postLogWithParam:dataList];

                }
            }];
        }else{
            [[BJNetManager shareNetManager]postRemoveBlockWithBlockUserId:self.userId completionHandler:^(id  _Nonnull responOj, NSError * _Nonnull error) {
                NSDictionary *dict = responOj;
                NSInteger code = [dict[@"code"]integerValue];
                if(!error && code == 0){
                    [self SuccessFullyWithTitle:blockTitle];
                }else{
                    [self UnsuccessFullyWithTitle:blockTitle];
                }
            }];
        }
        
    }];
    UIAlertAction *follow = [UIAlertAction actionWithTitle:followTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        if([followTitle isEqualToString:@"Follow"]){
            [[BJNetManager shareNetManager]postAddFriendWithFollowUserId:self.userId completionHandler:^(id  _Nonnull responseObj,NSError * _Nonnull error) {
                NSDictionary *dict = responseObj;
                NSInteger code = [dict[@"code"]integerValue];
                if(!error && code == 0){
                    [self SuccessFullyWithTitle:followTitle];
                    if(self.followClickBlock)self.followClickBlock(YES);
                }
            }];
        }else{
            [[BJNetManager shareNetManager]postDeleteFriendWithFollowUserId:[NSString stringWithFormat:@"%ld",self.userId] completionHandler:^(id  _Nonnull responseObj,NSError * _Nonnull error) {
                NSDictionary *dict = responseObj;
                NSInteger code = [dict[@"code"]integerValue];
                if(!error && code == 0){
                    [self SuccessFullyWithTitle:followTitle];
                    if(self.followClickBlock)self.followClickBlock(NO);
                }
            }];
        }
    }];
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil];
    [alertController addAction:follow];
    [alertController addAction:block];
    [alertController addAction:report];
    [alertController addAction:cancel];
    
    [[NERUtils getVisibleViewController] presentViewController:alertController animated:YES completion:nil];

//    [self presentViewController:alertController animated:true completion:nil];
    
}

- (void)reportSure{
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [param setObject:@(self.userId) forKey:@"broadcasterId"];
    [param setObject:@"Report" forKey:@"complainCategory"];
    UIAlertController *reportAlertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction *pornographic = [UIAlertAction
                                   actionWithTitle:@"Porngraphic" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [param setObject:@"Porngraphic" forKey:@"complatinSub"];
        [[BJNetManager shareNetManager] postBlockReportWithParam:param completionHandler:^(id  _Nonnull responOj, NSError * _Nonnull error) {
            if(!error){
                [self reportSuccessFully];
            }
        }];
    }];
    UIAlertAction *gender = [UIAlertAction actionWithTitle:@"Falsegender" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [param setObject:@"Porngraphic" forKey:@"False gender"];
        [[BJNetManager shareNetManager] postBlockReportWithParam:param completionHandler:^(id  _Nonnull responOj, NSError * _Nonnull error) {
            if(!error){
                [self reportSuccessFully];
            }
        }];
    }];
    UIAlertAction *fraud = [UIAlertAction actionWithTitle:@"Fraud" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [param setObject:@"Porngraphic" forKey:@"Fraud"];
        [[BJNetManager shareNetManager] postBlockReportWithParam:param completionHandler:^(id  _Nonnull responOj, NSError * _Nonnull error) {
            if(!error){
                [self reportSuccessFully];
            }
        }];
    }];
    UIAlertAction *political = [UIAlertAction actionWithTitle:@"Political Sensitive" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [param setObject:@"Porngraphic" forKey:@"Political sensitive"];
        [[BJNetManager shareNetManager] postBlockReportWithParam:param completionHandler:^(id  _Nonnull responOj, NSError * _Nonnull error) {
            if(!error){
                [self reportSuccessFully];
            }
        }];
    }];
    UIAlertAction *other = [UIAlertAction actionWithTitle:@"other" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [param setObject:@"Porngraphic" forKey:@"Other"];
        [[BJNetManager shareNetManager] postBlockReportWithParam:param completionHandler:^(id  _Nonnull responOj, NSError * _Nonnull error) {
            if(!error){
                [self reportSuccessFully];
            }
        }];
    }];
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"cancel" style:UIAlertActionStyleCancel handler:nil];
    [reportAlertController addAction:pornographic];
    [reportAlertController addAction:gender];
    [reportAlertController addAction:fraud];
    [reportAlertController addAction:political];
    [reportAlertController addAction:other];
    [reportAlertController addAction:cancel];
    [[NERUtils getVisibleViewController] presentViewController:reportAlertController animated:YES completion:nil];

//    [self presentViewController:reportAlertController animated:true completion:nil];
}

- (void)dimiss:(UIAlertController *)alert{
    [alert dismissViewControllerAnimated:YES completion:nil];
}

//Report Successfully弹窗
- (void)reportSuccessFully{
    [ASHUD showWithText:@"Report successfully" afterDelay:1.0];
}

- (void)SuccessFullyWithTitle:(NSString *)title{

    [ASHUD showWithText:[NSString stringWithFormat:@"%@ successfully",title] afterDelay:1.0];
}

- (void)UnsuccessFullyWithTitle:(NSString *)title{

    [ASHUD showWithText:[NSString stringWithFormat:@"%@ unsuccessfully",title] afterDelay:1.0];
}


@end
